import json
from dataclasses import dataclass


# @dataclass类似于lombok的@Data
@dataclass
class Blog(object):
    blog_id: str
    title: str
    content: str
    author: str

    def __init__(self):
        self.blog_id = ''
        self.title = ''
        self.content = ''
        self.author = ''

    # 遍历所有属性的key，方便转换成dict后打印成json
    def keys(self):
        return ('blog_id', 'title', 'content', 'author')

    def __getitem__(self, item):
        return getattr(self, item)

    def __str__(self):
        return json.dumps(dict(self.__dict__))

    def __repr__(self):
        return self.__str__()


if __name__ == '__main__':
    tt = Blog()
    print(tt)
    d = dict(tt)
    print(d)
